package com.lucun.es_transaction.service.impl;
import com.lucun.es_transaction.entity.Commodity;
import com.lucun.es_transaction.entity.Orders;
import com.lucun.es_transaction.entity.UserInfo;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.CommodityMapper;
import com.lucun.es_transaction.mapper.OrdersMapper;
import com.lucun.es_transaction.mapper.UserInfoMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.OrdersService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    UserLogMapper userLogMapper;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    Commodity commodity=new Commodity();
    @Override
    public JsonResult<?> insert(Orders orders, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(orders)) {
            throw new BaseException("数据不能为空");
        }
        String numberByToken = JwtUtil.getNumberByToken(request);
        if (ObjectUtils.isEmpty(numberByToken)) {
            throw new BaseException("解析tokenNumber失败");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idByToken)) {
            throw new BaseException("解析tokenId失败");
        }
        orders.setUserId(idByToken);
        orders.setComNumber(orders.getComNumber());//商品编号
        int num = 0;
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            num = random.nextInt(9);
            stringBuffer.append(num);
        }
        SimpleDateFormat y = new SimpleDateFormat("yy");
        String timeStr = y.format(new Date());
        orders.setOrderid(timeStr + stringBuffer.toString() + numberByToken + "4639");//生成订单编号
        if (ObjectUtils.isEmpty(orders.getOrderid())) {
            throw new BaseException("订单编号不能为空");
        }
        if (ObjectUtils.isEmpty(orders.getNum())) {
            throw new BaseException("商品数量不能为空");
        }
        int s=commodityMapper.selectnumber(Integer.parseInt(orders.getComNumber()));
        int sale=commodityMapper.selectsale(Integer.parseInt(orders.getComNumber()));
        int number = orders.getNum();
        if(number<1){
            throw new BaseException("商品库存不足");
        }
        if (number>s) {
            throw new BaseException("商品库存不足");
        }else {
           s=s-number;
            commodity.setComTotal(s);
            commodity.setComNumber(orders.getComNumber());
            commodity.setComSale(sale+number);
            commodityMapper.updateBytotal(commodity);
            System.out.println(orders.getComNumber()+"剩余库存"+number);
        }
        orders.setPrice(orders.getComPrice() * orders.getNum());
        if (ObjectUtils.isEmpty(orders.getPrice())) {
            throw new BaseException("实付款不能为空");
        }
        orders.setCreateTime(new Date());//创建时间
        orders.setStatus((byte) 0);//发货状态默认为待发货
        orders.setPayment((byte) 0);//支付状态默认为未支付
        Double byprice = ordersMapper.selectByprice(idByToken);
        if (byprice==null){
            byprice= Double.valueOf(orders.getPrice());
        }
        if (ObjectUtils.isEmpty(byprice)){
            throw new BaseException("获取不到总金额");
        }
        UserInfo record = new UserInfo();
        record.setUpdateTime(new Date());
        record.setNumber(numberByToken);
        record.setConsumption(BigDecimal.valueOf(byprice));
        Byte level1 = 3;
        if (byprice >= 500 || byprice < 1000) {
            level1 = 2;
        }
        if (byprice >= 1000) {
            level1 = 1;
        }
        record.setLevel(level1);
        userInfoMapper.updateByPrimaryKeySelective(record);
        int i = ordersMapper.insertSelective(orders);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (i > 0) {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "下单购买" + orders.getComName() + "商品" + "成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "下单购买" + orders.getComName() + "商品" + "失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200", "添加成功", orders);
    }
    @Override
    public JsonResult<?> updateByPrimaryKey(Orders orders, HttpServletRequest request) {
        return null;
    }

    @Override
    public JsonResult<?> updateByPrimaryKeySelective(Orders orders, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUpdateTime(new Date());
        int i=ordersMapper.updateByPrimaryKeySelective(orders);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+orders.getOrderid()+"订单收货地址"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+orders.getOrderid()+"订单收货地址"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","修改成功",orders);
    }
    @Override
    public JsonResult<?> updateBypayment(Orders orders, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUpdateTime(new Date());
        int i=ordersMapper.updateBypayment(orders);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+orders.getOrderid()+"订单付款状态"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+orders.getOrderid()+"订单付款状态"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","修改成功",orders);
    }
    @Override
    public JsonResult<?> updateBystatus(Orders orders, HttpServletRequest request) {//订单发货状态
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUpdateTime(new Date());
        int i=ordersMapper.updateBystatus(orders);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+orders.getOrderid()+"订单发货状态"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+orders.getOrderid()+"订单发货状态"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","修改成功",orders);
    }
    @Override
    public JsonResult<?> updateByreceiving(Orders orders, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUpdateTime(new Date());

        int i=ordersMapper.updateBystatus(orders);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"收货"+orders.getOrderid()+"订单"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"收货"+orders.getOrderid()+"订单"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","收货成功",orders);
    }

    @Override
    public JsonResult<?> updateBypaymentTime(Orders orders, HttpServletRequest request) {
        return null;
    }

    @Override
    public JsonResult<?> selectByPrimaryKey(Orders orders, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if (ObjectUtils.isEmpty(idByToken)) {
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        Orders i = ordersMapper.selectByPrimaryKey(orders);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(orders)) {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "订单" + "失败");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "订单" + "成功");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(orders)) {
            throw new BaseException("数据查询失败");
        }
        return new JsonResult("200", "查询成功", i);
    }

    @Override
    public JsonResult<?> selectByUserId(Orders orders, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if (ObjectUtils.isEmpty(idByToken)) {
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUserId(idByToken);
        List<Orders> i = ordersMapper.selectByUserId(orders);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(orders)) {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "订单" + "失败");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "订单" + "成功");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(orders)) {
            throw new BaseException("数据查询失败");
        }
        return new JsonResult("200", "查询成功", i);
    }
    @Override
    public Double selectByprice(Orders id, HttpServletRequest request) {
        return null;
    }

    @Override
    public JsonResult<?> selectBystatus(Orders orders, HttpServletRequest request) {//查询待发货订单
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);

        if (ObjectUtils.isEmpty(idByToken)) {
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUserId(idByToken);
        List<Orders> i = ordersMapper.selectBystatus(orders);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(orders)) {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "待发货订单" + "失败");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "待发货订单" + "成功");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(orders)) {
            throw new BaseException("数据查询失败");
        }
        return new JsonResult("200", "查询成功", i);
    }

    @Override
    public JsonResult<?> selectBypayment(Orders orders, HttpServletRequest request) {//待付款订单查询
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);

        if (ObjectUtils.isEmpty(idByToken)) {
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUserId(idByToken);
        List<Orders> i = ordersMapper.selectBypayment(orders);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(orders)) {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "待付款订单" + "失败");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "待付款订单" + "成功");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(orders)) {
            throw new BaseException("数据查询失败");
        }
        return new JsonResult("200", "查询成功", i);
    }

    @Override
    public JsonResult<?> selectByreceiving(Orders orders, HttpServletRequest request) {//待收货订单查询
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);

        if (ObjectUtils.isEmpty(idByToken)) {
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        orders.setUserId(idByToken);
        List<Orders> i = ordersMapper.selectByreceiving(orders);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(orders)) {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "待收货订单" + "失败");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "待收货订单" + "成功");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(orders)) {
            throw new BaseException("数据查询失败");
        }
        return new JsonResult("200", "查询成功", i);
    }
    @Override
    public JsonResult<?> deleteByPrimaryKey(Orders id, HttpServletRequest request) {
        String numberByToken = JwtUtil.getNumberByToken(request);
        int idByToken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idByToken)) {
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        Orders orders = ordersMapper.selectByPrimaryKey(id);
        int i = ordersMapper.deleteByPrimaryKey(id);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(i)) {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "删除" + orders.getOrderid() + "订单" + "失败");
            userLogMapper.insert(log);
            return new JsonResult("200", "删除失败");
        } else {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + orders.getOrderid() + "订单" + "成功");
            return new JsonResult("200", "删除成功");
        }

//
//    @Override
//    public JsonResult<?> selectByAll(Orders orders, HttpServletRequest request) {
//        return null;
//    }
    }
}
